package manager.service;

import manager.dao.OtherRouteDao;
import manager.domain.RouteInfo;

public class RouteService {
    private OtherRouteDao otherRouteDao =new OtherRouteDao();
    public boolean addRoute(RouteInfo routeInfo) {
//        老师对象交给RouteDao库管
//        RouteDao routeDao=new RouteDao();
//库管来找老师是否存在
        return otherRouteDao.addRoute(routeInfo);
    }

    public boolean isExists(String id) {
//        RouteDao routeDao=new RouteDao();
        RouteInfo[] routeInfos = otherRouteDao.findAllRoute(1);
//        假设id不存在
        boolean exists=false;
//        遍历数组
        for (int i = 0; i < routeInfos.length; i++) {
            RouteInfo routeInfo = routeInfos[i];
            if(routeInfo !=null&& routeInfo.getId().equals(id)){
                exists=true;
                break;
            }
        }
        return exists;
    }

    public RouteInfo[] findAllRoute(int route) {
//        用RouteDao获取老师数组
        RouteInfo[] allRouteInfo = otherRouteDao.findAllRoute(route);
        boolean flag=false;
        lo1:if (allRouteInfo != null){
            for (int i = 0; i < allRouteInfo.length; i++) {
                RouteInfo routeInfo = allRouteInfo[i];
                if(routeInfo !=null){
                    flag=true;
                    break lo1;
                }
            }
        }

        if(flag==true){
            return allRouteInfo;
        }else {
            return null;
        }
    }

    public void updateRoute(String updateId, RouteInfo newroute) {
        otherRouteDao.updateRoute(updateId,newroute);
    }

    public int[] buyTickets(String start, String end) {
        int[] infomation =new int[2];
        int price = 0;
        if(start.equals(end)){
            infomation[0] = price;
            infomation[1] = 0;
            return infomation;
        }else{
            String[][] result = otherRouteDao.buyTickets(start,end);
            String[] startarea = result[0];
            String[] endarea = result[1];
            if (startarea[1].equals(endarea[1])){
                price = Math.abs(Integer.valueOf(startarea[0])-Integer.valueOf(endarea[0]));
                infomation[0] = price;
                infomation[1] = 0;
            }else {
                if (startarea[1].equals("一号线")){
                    int price1 = Math.abs(13-Integer.valueOf(startarea[0]));
                    int price2 = Math.abs(17-Integer.valueOf(endarea[0]));
                    price = price1+price2;
                }else {
                    int price1 = Math.abs(13-Integer.valueOf(endarea[0]));
                    int price2 = Math.abs(17-Integer.valueOf(startarea[0]));
                    price = price1+price2;
                }
                infomation[0] = price;
                infomation[1] = 1;
            }
            return infomation;
        }

    }
}
